package leetcode

class Solution_67 {
    fun addBinary(a: String, b: String): String {
        var a = a
        var b = b
        val aLen = a.length
        val bLen = b.length
        val diff = aLen - bLen
        var max = aLen
        if (diff > 0){
           b = cheng(diff) + b
        }else{
            a = cheng(-diff) + a
            max = bLen
        }
        var sb = ""
        var up = 0
        for(ele in max - 1  downTo 0){
            val a1 = "${a[ele]}".toInt()
            val b1 = "${b[ele]}".toInt()
            when(up + a1 + b1){
                0 -> {
                    sb = "0" + sb
                    up = 0
                }
                1 -> {
                    sb = "1" + sb
                    up = 0
                }
                2 -> {
                    sb = "0" + sb
                    up = 1
                }
                3 -> {
                    sb = "1" + sb
                    up = 1
                }
            }
        }
        if (up == 1){
            sb = "1" + sb
        }
        return sb
    }
}
fun cheng(num :Int):String{
    var str = ""
    for (i in 0 until num) {
        str += "0"
    }
    return str
}

fun main() {
    println(Solution_67().addBinary("1111", "1111"))
}